//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using CommunityServer.Components;
using CommunityServer.Files.Components;

namespace CommunityServer.Files.Components
{
	/// <summary>
	/// Summary description for AggregateRssHandler.
	/// </summary>
	public class AggregateRssHandler : BaseFileGallerySyndicationHandler
	{
		public AggregateRssHandler()
		{
			//
			// TODO: Add constructor logic here
			//
		}

        protected Group CurrentGroup = null;
		protected bool SortByDownloads = false;

        protected bool HasGroup
        {
            get
            {
                return CurrentGroup != null;
            }
        }

        protected override void ProcessFeed()
        {
            int groupID = Globals.SafeInt(Context.Request.QueryString["GroupID"],-1);
            if(groupID > -1)
                CurrentGroup = Folders.GetFolderGroup(groupID,true,false);

			SortByDownloads = Globals.SafeBool(Context.Request.QueryString["Downloads"], false);

            base.ProcessFeed ();
        }

        protected override Section GetCurrentSection
        {
            get
            {
                return null;
            }
        }


	    protected override string CacheKey
	    {
	        get
	        {
				string key;
                if(!HasGroup)
                    key = string.Format("FilesAggregate:{0}:IndividualRss",CSContext.Current.SiteSettings.SettingsID);
                else
                    key = string.Format("FilesAggregate:{0}:GroupID{1}",CSContext.Current.SiteSettings.SettingsID,CurrentGroup.GroupID);

				if(SortByDownloads)
					key = string.Concat(key, ":Downloads");

				return key;
			}
	    }

	    protected override CachedFeed BuildFeed()
	    {
            FileGalleryThreadQuery query = new FileGalleryThreadQuery();
			query.ApplicationPostType = FileGalleryPostType.File;
			query.IncludeCategories = true;
            query.PageIndex = 0;
            query.PageSize = FileGalleryConfiguration.GetConfig().AggregatePostCount;
			query.FirstPageOnly = false;

			if(SortByDownloads)
				query.SortBy = EntriesSortBy.Downloads;
			else
				query.SortBy = EntriesSortBy.PostDate;
			
			query.SortOrder = SortOrder.Descending;
			query.OnlyApproved = true;
			//query.PostConfig = BlogPostConfig.IsCommunityAggregated | BlogPostConfig.IsAggregated;
			query.FilterByList = Sections.FilterByAccessControl(Folders.GetFolders(),Permission.View);
           
            if(HasGroup)
                query.GroupID = CurrentGroup.GroupID;

            ThreadSet threads = Entries.GetEntries(query,true);

			// Filter out any posts that belong to a blog that is not community aggregated
			ArrayList posts = threads.Threads;

			BaseRssWriter writer = null;
            writer = new AggregateRssWriter(posts,this.CurrentGroup,this.BaseUrl);
           
            DateTime dt =  posts.Count > 0 ? ((Entry)posts[0]).PostDate : DateTime.Now;
            
            return new CachedFeed(dt,null,writer.GetXml());
	    }
	}
}
